package com.central.scheduler.dao.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.central.db.mapper.SuperMapper;
import com.central.scheduler.common.enums.ExecutionStatus;
import com.central.scheduler.dao.model.ProcessDefinition;
import com.central.scheduler.dao.model.ProcessInstance;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author Tindy
 * @date 2021/11/1
 * @describe
 * process instance mapper interface
 */
@Mapper
public interface ProcessInstanceMapper extends SuperMapper<ProcessInstance> {
    /**
     * query process instance by host and stateArray
     *
     * @param host host
     * @param stateArray stateArray
     * @return process instance list
     */
    List<ProcessInstance> queryByHostAndStatus(@Param("host") String host,
                                               @Param("states") ExecutionStatus[] stateArray);
    /**
     * query last scheduler process instance
     *
     * @param definitionCode definitionCode
     * @param startTime startTime
     * @param endTime endTime
     * @return process instance
     */
    ProcessInstance queryLastSchedulerProcess(@Param("processDefinitionCode") Long definitionCode,
                                              @Param("startTime") Date startTime,
                                              @Param("endTime") Date endTime);

    /**
     * query last running process instance
     *
     * @param definitionCode definitionCode
     * @param startTime startTime
     * @param endTime endTime
     * @param stateArray stateArray
     * @return process instance
     */
    ProcessInstance queryLastRunningProcess(@Param("processDefinitionCode") Long definitionCode,
                                            @Param("startTime") Date startTime,
                                            @Param("endTime") Date endTime,
                                            @Param("states") ExecutionStatus[] stateArray);

    /**
     * query last manual process instance
     *
     * @param definitionCode definitionCode
     * @param startTime startTime
     * @param endTime endTime
     * @return process instance
     */
    ProcessInstance queryLastManualProcess(@Param("processDefinitionCode") Long definitionCode,
                                           @Param("startTime") Date startTime,
                                           @Param("endTime") Date endTime);

    List<ProcessInstance> queryByProcessDefineCodeVersion(@Param("processDefinitionCode")long code,@Param("processDefinitionVersion") int version, @Param("limit")Integer limit);

    List<ProcessInstance> queryProcessInstances(Page<ProcessInstance> page,@Param("u") Map<String, Object> params);
}
